﻿using System;
using System.Text;
using System.Xml.Linq;
using SPQueryToSql.Extensions;

namespace SPQueryToSql.ApplicationObjects.CAML
{
    internal class FieldRef
    {
        public Guid ID;
        public string Name;
        public string Alias;
        public bool? Ascending;
        public string CreateURL;
        public string DisplayName;
        public bool? LookupId;
        public bool? Explicit;
        public string Format;
        public string Key;
        public string List;
        public string RefType;
        public string ShowField;
        public bool? TextOnly;
        public string Type;

        public FieldRef(XElement element)
        {
            ID = element.AttributeValueGuid("ID", Guid.Empty);
            Name = element.AttributeValueString("Name", string.Empty);
            Alias = element.AttributeValueString("Alias", string.Empty);
            CreateURL = element.AttributeValueString("CreateURL", string.Empty);
            DisplayName = element.AttributeValueString("DisplayName", string.Empty);
            Format = element.AttributeValueString("Format", string.Empty);
            Key = element.AttributeValueString("Key", string.Empty);
            List = element.AttributeValueString("List", string.Empty);
            RefType = element.AttributeValueString("RefType", string.Empty);
            ShowField = element.AttributeValueString("ShowField", string.Empty);
            Type = element.AttributeValueString("Type", string.Empty);
            LookupId = element.AttributeValueNullableBool("LookupId");
            Ascending = element.AttributeValueNullableBool("Ascending");
            Explicit = element.AttributeValueNullableBool("Explicit");
            TextOnly = element.AttributeValueNullableBool("TextOnly");
        }

        public override string ToString()
        {
            return ToString(0);
        }

        public string ToString(int tabQnt)
        {
            var sb = new StringBuilder();
            const string tab = @"    ";
            for (var i = 0; i < tabQnt; i++) sb.Append(tab);
            sb.Append("<FieldRef ");
            if (ID != Guid.Empty)
                sb.AppendFormat(@"ID=""{0}"" ", ID.ToString("B"));
            if (!string.IsNullOrEmpty(Name))
                sb.AppendFormat(@"Name=""{0}"" ", Name);
            if (!string.IsNullOrEmpty(Alias))
                sb.AppendFormat(@"Alias=""{0}"" ", Alias);
            if (!string.IsNullOrEmpty(CreateURL))
                sb.AppendFormat(@"CreateURL=""{0}"" ", CreateURL);
            if (!string.IsNullOrEmpty(DisplayName))
                sb.AppendFormat(@"DisplayName=""{0}"" ", DisplayName);
            if (!string.IsNullOrEmpty(Key))
                sb.AppendFormat(@"Key=""{0}"" ", Key);
            if (!string.IsNullOrEmpty(List))
                sb.AppendFormat(@"List=""{0}"" ", List);
            if (!string.IsNullOrEmpty(RefType))
                sb.AppendFormat(@"RefType=""{0}"" ", RefType);
            if (!string.IsNullOrEmpty(ShowField))
                sb.AppendFormat(@"ShowField=""{0}"" ", ShowField);
            if (!string.IsNullOrEmpty(Type))
                sb.AppendFormat(@"Type=""{0}"" ", Type);
            if (Ascending.HasValue)
                sb.AppendFormat(@"Ascending=""{0}"" ", Ascending == true ? "TRUE" : "FALSE");
            if (Explicit.HasValue)
                sb.AppendFormat(@"Explicit=""{0}"" ", Explicit == true ? "TRUE" : "FALSE");
            if (TextOnly.HasValue)
                sb.AppendFormat(@"TextOnly=""{0}"" ", TextOnly == true ? "TRUE" : "FALSE");
            sb.Append("/>");
            sb.Append(Environment.NewLine);
            return sb.ToString();
        }
    }
}
